package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.SuggestMsgReq;
import sz.szsmk.citizencard.model.network.bean.request.SuggestReq;
import sz.szsmk.citizencard.model.network.bean.response.SuggestMsgRes;
import sz.szsmk.citizencard.mvp.contract.ISuggestContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/8/9 20:39
 * description:
 */
public class SuggestPresenter extends IBasePresenter
        implements ISuggestContact.ISuggestPresenter<ISuggestContact.ISuggestView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SuggestPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void submitSutgget(String phone, String content, String versionName) {
        SuggestReq suggestReq = new SuggestReq();

        String androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");


        OkHttpUtils.post()
                .url(Constant.BASE_URL + "app/config/submitUserFeedbackNoPic")
                .addParams("access_token", access_token)
                .addParams("app_info", versionName)
                .addParams("device_id", androidId)
                .addParams("phone", phone)
                .addParams("sign", "sdg")
                .addParams("soft_info", "asdf")
                .addParams("question_content", content)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        L.e(Constant.MTAG, "提交意见反馈结果：" + e.getMessage());
                        ((ISuggestContact.ISuggestView) mView).submitSutggetFail();
                    }

                    @Override
                    public void onResponse(String response) {
                          L.e(Constant.MTAG, "提交意见反馈结果：" + response);
                        ((ISuggestContact.ISuggestView) mView).submitSutggetSucc();

                    }
                });
//                .execute(new StringCallback() {
//                    @Override
//                    public Object parseNetworkResponse(Response response) throws IOException {
//                        return null;
//                    }
//
//                    @Override
//                    public void onError(Request request, Exception e) {
//                        ((ISuggestContact.ISuggestView) mView).submitSutggetFail();
//                    }
//
//                    @Override
//                    public void onResponse(String response) {
//
//                    }
//
//                    @Override
//                    public void onResponse(Object response) {
//                        ((ISuggestContact.ISuggestView) mView).submitSutggetSucc();
//                    }
//                });
//        if (access_token != null) {
//            suggestReq.setAccess_token(access_token);
//        }
//        suggestReq.setPhone(phone);
//        suggestReq.setApp_info(versionName);
//        suggestReq.setDevice_id(androidId);
//        suggestReq.setQuestion_content(content);
//        suggestReq.setSign("asd");
//        suggestReq.setSoft_info("fhj");
//        String json = new Gson().toJson(suggestReq);
//        L.e(Constant.TAG,"submitUserFeedbackNoPic="+json);


    }

    @Override
    public void getSuggestReply() {
        SuggestMsgReq msgReq = new SuggestMsgReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        String androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());
        msgReq.setAccess_token(access_token);
        msgReq.setDevice_id(androidId);
        msgReq.setSign("asdfasd");
        String s = new Gson().toJson(msgReq);
        L.e(Constant.TAG, "getSuggestReply---->" + s);
        mNetworkDateSource.queryUserFeedbackAnswer(msgReq, new SubscribeResult<SuggestMsgRes>() {
            @Override
            protected void onOk(SuggestMsgRes response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "getSuggestReply---->" + json);
                ((ISuggestContact.ISuggestView) mView).getReplySucc(response);
            }

            @Override
            protected void onFailure(SuggestMsgRes response) {
                super.onFailure(response);
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "getSuggestReply---->" + json);
                ((ISuggestContact.ISuggestView) mView).getReplyFail(response.getMessage());
            }

            @Override
            protected void onNext(SuggestMsgRes response) {
                super.onNext(response);
                ((ISuggestContact.ISuggestView) mView).getReplyFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISuggestContact.ISuggestView) mView).getReplyFail(throwable.getMessage());

            }
        });
    }

}
